package com.java.basic;

import java.util.concurrent.CountDownLatch;

public class CountDownLatchTest {

    public  static void main(String[] args){
        /**
         * 当countDown减到0时，await等待的线程唤醒继续执行
         */
        final CountDownLatch cdl = new CountDownLatch(3);
        new Thread(
                    new Runnable() {
                        public void run() {
                            System.out.println("Thread start:"+Thread.currentThread().getName());
                            try {
                                Thread.sleep(5000);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            cdl.countDown();
                            System.out.println("Thread end:"+Thread.currentThread().getName());
                        }
                    }
            ).start();

        for(int i=0;i<10;i++) {
            new Thread(
                    new Runnable() {
                        public void run() {
                            try {
                                System.out.println("线程"+Thread.currentThread().getName()+"开始等待...");
                                cdl.await();
                                System.out.println("线程"+Thread.currentThread().getName()+"等待结束...");
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    }
            ).start();

        }
    }
}
